local BagLogic = require("bag.BagLogic")
local Grid = require("bag.Grid")
local MailLogicFL = require("mail.MailLogicFL")

function onSendHolidayGift(human, data)
    local nGiftList = data.giftlist
    if nGiftList then
        local nList = {}
        for k, v in ipairs(nGiftList) do
            nList[k] = Grid.create(nil, v[1], v[2], v[3] or 1)  
        end

        local nNeedSpace = #nGiftList 
        local nLeftBag = BagLogic.getLeftGrids(human)
        if nNeedSpace > nLeftBag then
            local nTempList = {}
            local nLen = 0
            for k, v in ipairs(nList) do
                nLen = nLen + 1
                nTempList[k] = v
                if nLen >= nNeedSpace or k % 6 == 0 then
                    MailLogicFL.CreateNewMailForUser(human._id, "合服活动节日礼包", "由于您的背包已满，特将奖励发给您", nTempList)
                    nTempList = {}
                end
            end
            return
        end

        for k, v in ipairs(nList) do
            BagLogic.addItemByGrid(human, nil, v, "complexactholidaygift")
        end
    end
end

